#!/usr/bin/env node

const router = require('koa-router')(),
      users = require('../models/users'),
      jwt = require('jsonwebtoken'),
      _   = require('lodash'),
      { JWT_KEY } = require('../lib/constant'),
      log = console.log;

router.prefix('/api/users');


router.post('/login', async (ctx) => {
  let { email, password } = ctx.request.body;
  
  let res = await users.login(email,password);

  if(res.code === 0){
    res.data = await jwt.sign(_.toPlainObject(res.data),JWT_KEY);
  }
  ctx.body = res;         
})


module.exports = router;
